package com.yc.blog.springboot.mixview.ctr;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 跳转thymeleaf页面
 */
@Controller
public class DemoController1 {

	/**
	 * 跳转页面<p>
	 * 方式：返回ModelAndView<p>
	 * @return
	 */
	@RequestMapping("/c1-1")
	public ModelAndView c1() {
		System.out.println("--------c1-1---------");
		
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("page2");//指定页面
		
		mv.addObject("para1", "aaaaa");//绑定参数1
		mv.addObject("para2", "bbbbb");//绑定参数2
		
		return mv;
	}
	
	/**
	 * 重定向<p>
	 * 现象：地址改变
	 * @return
	 */
	@RequestMapping("/c1-2")
	public ModelAndView c2() {
		System.out.println("--------c1-2---------");
		ModelAndView mv = new ModelAndView("redirect:/page1.html");
		//参数无效
		mv.addObject("para1", "aaaaa1");
		mv.addObject("para2", "bbbbb1");
		return mv;
	}
	
	/**
	 * 转发<p>
	 * 现象：地址不变，URL上会拼接上参数
	 * 注意：如果不写forward:，默认会去templates目录里找页面。写上forward:才会去static目录里去找
	 * @return
	 */
	@RequestMapping("/c1-3")
	public ModelAndView c3() {
		System.out.println("--------c1-3---------");
		//虽然默认为forward模式 ，但这里如果不写  forward:还是会报错，因为会首先去templates里去找
		ModelAndView mv = new ModelAndView("forward:/page1.html");
		//参数会拼接在URL里
		mv.addObject("para1", "aaaaa1");
		mv.addObject("para2", "bbbbb1");
		return mv; 
	}
	
	/**
	 * 转发到另外一个请求
	 * @return
	 */
	@RequestMapping("/c1-4")
	public ModelAndView c4() {
		System.out.println("--------c1-4---------");
		ModelAndView mv = new ModelAndView("forward:/c1-2");
		return mv; 
	}

}
